<?php

/**
 * This file contains common functions
 * deal with util stuff
 */
//function cleanInput($input) {
//    $input = trim($input);
//    $input = mysql_real_escape_string($input);
//
//    $input = str_replace("\"", "", $input);
//    $input = str_replace("'", "", $input);
//
//    return $input;
//}

function generateBreadCrum( $action ){
    $result = array( '首页' => 'index.php' );

    switch ($action){
        case "item":
            $result['详细内容页'] = '';
            break;
        case "search":
            $result['搜索'] = '';
            break;
        case "category":
            $result['分类'] = '';
            break;
    }

    return $result;
}

/**
 * redirect function
 * 
 * @param <type> $url
 * @param <type> $seconds
 * @return <type>
 */
function js_redirect( $url, $seconds=3 ) {
    echo "<script language=\"JavaScript\">\n";
    echo "<!-- hide code from displaying on browsers with JS turned off\n\n";
    echo "function redirect() {\n";
    echo "window.location = \"" . HOST.$url . "\";\n";
    echo "}\n\n";
    echo "timer = setTimeout('redirect()', '" . ($seconds * 1000) . "');\n\n";
    echo "-->\n";
    echo "</script>\n";

    return true;
}

/**
 * Clean user input
 * @param type $param
 * @return type 
 */
function cleanInput( $param ){
    if (is_array($param)){
        foreach ($param as $k => $v){
            $param[$k] = cleanInput($v); //recursive
        }
    }
    elseif (is_string($param)){
        $param = trim($param);
        
        // filter XSS
        $param = htmlspecialchars( $param );
        // filter SQL injection
        $trans = array(
            '"' => '&quot;',
            '\'' => ''
        );
        $param = strtr($param,$trans);
    }
    return $param;
}

/**
 * output json result
 * 
 * @param type $iRetcode
 * @param type $sErrorMsg
 * @param type $vmResult 
 */
function jsonp_output($funcName, $iRetcode, $sErrorMsg, $vmResult = array()) {
    $res = array(
        'retCode' => $iRetcode,
        'retInfo' => $sErrorMsg,
        'list' => $vmResult
    );

    echo ";".$funcName."(".json_encode($res).")";
}

/**
 * GBK转UTF8，传入的数据可为数组或字符串
 * 数组则继续解析到字符串
 * @param $str
 * @return unknown_type
 */
function GBKtoUTF8($str)
{
    if(is_array($str))
    {
        foreach ($str as &$value) 
        {
            $value = GBKtoUTF8($value);
        }
        return $str;
    }
    elseif (is_string($str))
    {   
        $str = iconv("GBK", "UTF-8//IGNORE", $str);
        return $str;
    }
    else
    {
        return $str;
    }
}

/**
 * UTF8转GBK，传入的数据可为数组或字符串
 * 数组则继续解析到字符串
 * @param $str
 * @return unknown_type
 */
function UTF8toGBK(&$str)
{
    if(is_array($str))
    {
        foreach ($str as &$value) 
        {
            $value = UTF8toGBK($value);
        }
        return $str;			
    }
    elseif (is_string($str))
    {   
        $str = iconv("UTF-8", "GBK//IGNORE", $str);
        return $str;
    }
    else
    {
        return $str;
    }
}

/**
 * 字符串起始匹配
 * 
 * @param type $haystack
 * @param type $needle
 * @return type 
 */
function startswith($haystack, $needle) {
    return strtolower(substr($haystack, 0, strlen($needle))) === strtolower( $needle );
}
?>
